package net.entelijan.cobean.examples.showcase.config.text;

import net.entelijan.cobean.data.literature.LitAuthor;
import net.entelijan.cobean.examples.showcase.ShowcaseTestFrame;
import net.entelijan.cobean.examples.util.ExamplesUtil;
import net.entelijan.testutil.CobeanTestUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.fest.swing.fixture.FrameFixture;
import org.fest.swing.fixture.JTextComponentFixture;
import org.junit.Assert;
import org.junit.Test;

public class TextBookAuthorFestTestCase {

	@SuppressWarnings("unused")
	private static Log log = LogFactory
			.getLog(TextBookAuthorFestTestCase.class);

	@Test
	public void testInit() {
		ConfigBookAuthorTextPanel panel = new ConfigBookAuthorTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ConfigBookAuthorTextShowcase sc = new ConfigBookAuthorTextShowcase();
			sc.setService(ExamplesUtil.createTestService());
			JTextComponentFixture authorNameTextBox = frameFix
					.textBox("authorNameTextField");
			JTextComponentFixture authorBirthdayTextBox = frameFix
					.textBox("authorBirthdayTextField");
			sc.init(panel);
			pause(100);

			authorNameTextBox.requireText("V.S. Naipaul");
			authorBirthdayTextBox.requireText("1932-08-17");

		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSetValuesOnGUI() {
		ConfigBookAuthorTextPanel panel = new ConfigBookAuthorTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ConfigBookAuthorTextShowcase sc = new ConfigBookAuthorTextShowcase();
			sc.setService(ExamplesUtil.createTestService());
			JTextComponentFixture failureTextBox = frameFix
					.textBox("failureTextField");
			JTextComponentFixture authorNameTextBox = frameFix
					.textBox("authorNameTextField");
			JTextComponentFixture authorBirthdayTextBox = frameFix
					.textBox("authorBirthdayTextField");
			sc.init(panel);
			pause(200);

			authorNameTextBox.deleteText();
			authorNameTextBox.setText("HALLO");
			authorBirthdayTextBox.focus();
			pause(100);

			Assert.assertEquals("HALLO", sc.getBook().getAuthor().getName());

			authorBirthdayTextBox.deleteText();
			authorBirthdayTextBox.setText("oaoao");
			authorNameTextBox.focus();
			pause(100);

			Assert.assertEquals(CobeanTestUtil.createDate("yyyyMMdd",
					"19320817"), sc.getBook().getAuthor().getBirthday());
			failureTextBox
					.requireText("'oaoao' is not a valid date. Use 'yyyy-MM-dd'");

			authorBirthdayTextBox.deleteText();
			authorBirthdayTextBox.setText("2001-01-01");
			authorNameTextBox.focus();
			pause(100);

			Assert.assertEquals(CobeanTestUtil.createDate("yyyyMMdd",
					"20010101"), sc.getBook().getAuthor().getBirthday());

		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSetValueOnModel() {
		ConfigBookAuthorTextPanel panel = new ConfigBookAuthorTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ConfigBookAuthorTextShowcase sc = new ConfigBookAuthorTextShowcase();
			sc.setService(ExamplesUtil.createTestService());
			JTextComponentFixture authorNameTextBox = frameFix
					.textBox("authorNameTextField");
			JTextComponentFixture authorBirthdayTextBox = frameFix
					.textBox("authorBirthdayTextField");

			sc.init(panel);
			pause(200);

			LitAuthor author = sc.getBook().getAuthor();
			author.setName("Hmmmmmmmm");
			pause(100);

			authorNameTextBox.requireText("Hmmmmmmmm");

			author.setBirthday(CobeanTestUtil
					.createDate("yyyyMMdd", "19990202"));
			pause(100);

			authorBirthdayTextBox.requireText("1999-02-02");

			LitAuthor a = new LitAuthor();
			a.setName("NAMME");
			a.setBirthday(CobeanTestUtil.createDate("yyyyMMdd", "19750303"));
			sc.getBook().setAuthor(a);
			pause(100);
			authorNameTextBox.requireText("NAMME");
			authorBirthdayTextBox.requireText("1975-03-03");

			sc.getBook().getAuthor().setName("NUMME");
			pause(100);
			authorNameTextBox.requireText("NUMME");

		} finally {
			frameFix.cleanUp();
		}

	}

	private synchronized void pause(int timeMs) {
		try {
			wait(timeMs);
		} catch (InterruptedException e) {
			// Do not mind
		}

	}

}
